package refelcetdemo;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Test {
    public static void main(String[] args) {
        try {
            Class<?> c1 = Class.forName("refelcetdemo.Student");
            Method method =
                    c1.getDeclaredMethod("function", String.class);
            method.setAccessible(true);
            Student student = (Student) c1.newInstance();
            method.invoke(student,"调用成功");
        }catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }
    public static void main4(String[] args) {
        try {
            Class<?> c1 = Class.forName("refelcetdemo.Student");
            Field field = c1.getField("age");
            field.setAccessible(true);
            Student student = (Student) c1.newInstance();
            field.set(student,9);
            System.out.println(student);
        }catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main3(String[] args) {
        try {
            Class<?> c1 = Class.forName("refelcetdemo.Student");
            Constructor<Student> constructor =
                    (Constructor<Student>) c1.getDeclaredConstructor(String.class,int.class );
            constructor.setAccessible(true);
            Student student = constructor.newInstance("小明",6);
            System.out.println(student);
        }catch (ClassNotFoundException | NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
    public static void main2(String[] args) {
        try{
            Class<?> c1 = Class.forName("refelcetdemo.Student");
            Student student = (Student) c1.newInstance();
            System.out.println(c1);
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
    public static void main1(String[] args) throws ClassNotFoundException {

        try{
            Class<Student> c1 = Student.class;
            System.out.println(c1);

            Student s = new Student();
            Class<? extends Student> c2 = s.getClass();
            System.out.println(c2);

            Class<?> c3 = null;
            c3 = Class.forName("refelcetdemo.Student");
            System.out.println(c3);
        }catch(ClassNotFoundException e){
            e.printStackTrace();
        }


    }
}
